package com.ofek.eventos;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.ofek.applicationbase.broadcast.Delegate;
import com.ofek.eventos.db.EventosDatabaseService;
import com.ofek.eventos.db.EventosDatabaseService.DataListener;
import com.ofek.eventos.db.EventosDatabaseService.EventosDatabaseServiceBinder;
import com.ofek.eventos.db.datatypes.EventData;
import com.ofek.eventos.db.datatypes.SearchDefiner;
import com.ofek.eventos.db.datatypes.predefined.PageDefiner;

public class PageFragment extends EventsFragment implements ServiceConnection, DataListener {
	private static final String SEARCH_DEFINER = "SEARCH_DEFINER";

	public EventListAdapter listAdapter;
	private EventosDatabaseService eventosService;
	private Activity dactivity;
	private SearchDefiner searchScheme;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try{searchScheme = (SearchDefiner) getArguments().getSerializable(SEARCH_DEFINER);}catch(final Exception e) {}

	}


	/* (non-Javadoc)
	 * @see com.ofek.eventos.EventsFragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	@Override
	public void onResume() {
		super.onResume();
		dactivity = getActivity();
		dactivity.bindService(new Intent(dactivity,EventosDatabaseService.class), this, 0);
		if ( eventosService!=null ) 
			updateData(eventosService);

	}
	@Override
	public void onPause() {
		super.onPause();
		//		
		//		if ( eventosService!=null ) updateData(eventosService);
		dactivity.unbindService(this);
	}

	@Override
	public void onServiceConnected(final ComponentName name, final IBinder service) {
		final EventosDatabaseServiceBinder binder = (EventosDatabaseServiceBinder) service;
		eventosService = binder.getEventosService();
		eventosService.registerDataListener(this);
		updateData(eventosService);

	}
	@Override
	public void onUpdate(final EventData older, final EventData newer) {
		updateData(eventosService);
	}

	@Override
	public void onRemoval(final EventData removed) {
		updateData(eventosService);
	}

	@Override
	public void onAddition(final List<EventData> all) {
		updateData(eventosService);
	}

	@Override
	public void onAddition(final EventData added) {
		updateData(eventosService);
	}
	protected void updateData(final EventosDatabaseService eventosService2) {

		eventosService.searchEvents(searchScheme, new Delegate<ArrayList<EventData>>() {

			@Override
			public void invoke(final ArrayList<EventData> t) {
				if (!isResumed()) return;
				getActivity().runOnUiThread(new Runnable() {

					@Override
					public void run() {
						if  ( t==null ) return;
						setEventsToShow(t);
						setEdit(((PageActivity) dactivity).getEditState());
					}
				});

			}
		});
	}



	@Override
	public void onServiceDisconnected(final ComponentName name) {
		eventosService.unregisterDataListener(this);
	}

	public static PageFragment newInstance(final PageDefiner pageDefiner) {
		final PageFragment f = new PageFragment();
		final Bundle bdl = new Bundle(3);
		bdl.putSerializable(SEARCH_DEFINER, pageDefiner);
		f.setArguments(bdl);
		return f;
	}




}